#!/usr/bin/env python3
"""
检查项目结构是否完整的脚本
"""

import os
import json

def check_file_exists(file_path, description):
    """检查文件是否存在"""
    if os.path.exists(file_path):
        print(f"✅ {description}: {file_path}")
        return True
    else:
        print(f"❌ {description}: {file_path} (不存在)")
        return False

def check_directory_exists(dir_path, description):
    """检查目录是否存在"""
    if os.path.isdir(dir_path):
        print(f"✅ {description}: {dir_path}")
        return True
    else:
        print(f"❌ {description}: {dir_path} (不存在)")
        return False

def check_package_json():
    """检查package.json配置"""
    if not os.path.exists("package.json"):
        print("❌ package.json 不存在")
        return False
    
    try:
        with open("package.json", "r", encoding="utf-8") as f:
            data = json.load(f)
        
        # 检查必要的脚本
        scripts = data.get("scripts", {})
        required_scripts = ["tauri:dev", "tauri:build"]
        
        missing_scripts = []
        for script in required_scripts:
            if script not in scripts:
                missing_scripts.append(script)
        
        if missing_scripts:
            print(f"❌ package.json 缺少脚本: {', '.join(missing_scripts)}")
            return False
        else:
            print("✅ package.json 脚本配置正确")
            return True
            
    except Exception as e:
        print(f"❌ package.json 解析失败: {e}")
        return False

def check_tauri_config():
    """检查Tauri配置"""
    config_path = "src-tauri/tauri.conf.json"
    if not os.path.exists(config_path):
        print(f"❌ {config_path} 不存在")
        return False
    
    try:
        with open(config_path, "r", encoding="utf-8") as f:
            data = json.load(f)
        
        # 检查基本配置
        if "package" in data and "productName" in data["package"]:
            print(f"✅ Tauri配置正确，应用名称: {data['package']['productName']}")
            return True
        else:
            print("❌ Tauri配置不完整")
            return False
            
    except Exception as e:
        print(f"❌ Tauri配置解析失败: {e}")
        return False

def main():
    """主函数"""
    print("🔍 检查MediaCrawler Pro项目结构...")
    print("=" * 60)
    
    checks = []
    
    # 检查前端文件
    print("\n📁 前端文件检查:")
    checks.append(check_file_exists("package.json", "Node.js配置"))
    checks.append(check_file_exists("vite.config.ts", "Vite配置"))
    checks.append(check_file_exists("tsconfig.json", "TypeScript配置"))
    checks.append(check_directory_exists("src", "前端源码目录"))
    
    # 检查Tauri后端文件
    print("\n🦀 Tauri后端文件检查:")
    checks.append(check_directory_exists("src-tauri", "Tauri目录"))
    checks.append(check_file_exists("src-tauri/Cargo.toml", "Rust配置"))
    checks.append(check_file_exists("src-tauri/tauri.conf.json", "Tauri配置"))
    checks.append(check_file_exists("src-tauri/build.rs", "构建脚本"))
    
    # 检查Rust源码文件
    print("\n📄 Rust源码文件检查:")
    rust_files = [
        ("src-tauri/src/main.rs", "主入口文件"),
        ("src-tauri/src/commands.rs", "命令处理"),
        ("src-tauri/src/config.rs", "配置管理"),
        ("src-tauri/src/database.rs", "数据库操作"),
        ("src-tauri/src/crawler.rs", "爬虫引擎"),
        ("src-tauri/src/models.rs", "数据模型"),
        ("src-tauri/src/utils.rs", "工具函数"),
        ("src-tauri/src/error.rs", "错误处理"),
    ]
    
    for file_path, description in rust_files:
        checks.append(check_file_exists(file_path, description))
    
    # 检查数据库迁移
    print("\n🗄️ 数据库文件检查:")
    checks.append(check_directory_exists("src-tauri/migrations", "迁移目录"))
    checks.append(check_file_exists("src-tauri/migrations/001_initial.sql", "初始迁移"))
    
    # 检查配置文件
    print("\n⚙️ 配置文件检查:")
    checks.append(check_package_json())
    checks.append(check_tauri_config())
    
    # 检查文档文件
    print("\n📚 文档文件检查:")
    checks.append(check_file_exists("TAURI_README.md", "Tauri说明文档"))
    checks.append(check_file_exists("install_rust.md", "Rust安装指南"))
    
    # 统计结果
    passed = sum(checks)
    total = len(checks)
    
    print("\n" + "=" * 60)
    print("📊 检查结果:")
    print(f"✅ 通过: {passed}/{total}")
    print(f"❌ 失败: {total - passed}/{total}")
    
    if passed == total:
        print("\n🎉 项目结构完整！")
        print("\n📋 下一步操作:")
        print("1. 安装Rust: 参考 install_rust.md")
        print("2. 安装依赖: npm install")
        print("3. 启动开发: npm run tauri:dev")
    else:
        print(f"\n⚠️ 项目结构不完整，请检查缺失的文件。")
    
    return 0 if passed == total else 1

if __name__ == "__main__":
    exit(main())